
o = {
    default:{
    "n": 20,
    "codes": {
        "0": "W",
        "1": "l",
        "2": "k",
        "3": "B",
        "4": "Q",
        "5": "g",
        "6": "f",
        "7": "i",
        "8": "i",
        "9": "r",
        "10": "v",
        "11": "6",
        "12": "A",
        "13": "K",
        "14": "N",
        "15": "k",
        "16": "4",
        "17": "L",
        "18": "1",
        "19": "8"
    }
}
}

function get_key(vs) {

    for (var e = (vs.length > 0 && void 0 !== vs[0] ? vs[0] : "/").toLowerCase(), t = e + e, n = "", i = 0; i < t.length; ++i) {
        var a = t[i].charCodeAt() % o.default.n;
        n += o.default.codes[a]
    }
    return n
};

class RPC extends WebSocket{
    constructor(name,host,prot){
        let url = `ws://${host}:${prot}/ws`;
        super(url);

        this.name = name;
        this.funDic = {};
        this.onopen = function() {this.send(JSON.stringify({"msg":"register","browser":this.name}))}

        this.onmessage = function(msgEvent){
            console.log(msgEvent.data) ;
            var response = {"msg":"result","success":false}
            var data = {};
            if (msgEvent.data.match("exec")){
                data = JSON.parse(msgEvent.data);
            }

           // {"exec": fun, "arg": arg, "resultId": resultId}

            if (data["exec"]){

                let fun = this.funDic[data["exec"]]
                // 直接eval 要是接口对外开放可能存在风险
                if (!fun){
                    this.send(JSON.stringify(response));
                    return
                }
                let ret = fun.apply(fun,eval(data["arg"]));
                response["result"] = ret;
                response["resultId"] = data["resultId"];
                response["success"] = true;

                this.send(JSON.stringify(response));}
            }
        }
        register(f){this.funDic[f.name] = f;}
}


let getCookie = function (){
    return (new o).z(m, (new Date).getTime() + 60 * (480 + (new Date).getTimezoneOffset()) * 1e3)
}


let getHostName = function (a,b){
    return `hostname:${window.location.hostname} a:${a} b:${b} a+b=${a+b}`
}

window.__rpc = new RPC("浏览器A","127.0.0.1",5123)
window.__rpc.register(getHostName)
window.__rpc.register(getCookie)
